Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
¿Cómo puedo evitar que mi función trate "0" como vacío?

Tengo un formulario de búsqueda de lotería que concatena seis campos de entrada en un solo valor y devuelve los resultados de los boletos según la posición. Mi formulario funciona correctamente para los números del 1 al 9, pero no puede mostrar los resultados cuando busco el 0.

Creo que esto se debe a que dentro de mi función get_numbers(), el valor de '0' se trata como vacío: https://www.php.net/manual/en/function.empty.php

¿Cómo puedo arreglar mi declaración if else para evitar que trate 0 como vacío?

 add_filter('wp_ajax_nopriv_get_numbers', 'get_numbers', 10); add_filter('wp_ajax_get_numbers', 'get_numbers', 10); function get_numbers(){ $str = ''; if(!empty($_POST['n_1'])){ $str .= $_POST['n_1']; }else{ $str .= '.'; } if(!empty($_POST['n_2'])){ $str .= $_POST['n_2']; }else{ $str .= '.'; } if(!empty($_POST['n_3'])){ $str .= $_POST['n_3']; }else{ $str .= '.'; } if(!empty($_POST['n_4'])){ $str .= $_POST['n_4']; }else{ $str .= '.'; } if(!empty($_POST['n_5'])){ $str .= $_POST['n_5']; }else{ $str .= '.'; } if(!empty($_POST['n_6'])){ $str .= $_POST['n_6']; }else{ $str .= '.'; } $atts = array( 'numberposts' => -1, 'posts_per_page' => -1, 'post_type' => 'product', 'meta_query' => array( array( 'key' => 'product_overlay_text', 'value' => $str, 'compare' => 'REGEXP', ), ), ); $the_query = new WP_Query( $atts ); ob_start(); if($the_query->have_posts()) : woocommerce_product_loop_start(); while($the_query->have_posts()) : $the_query->the_post(); wc_get_template_part( 'content', 'product' ); endwhile; woocommerce_product_loop_end(); $content['tickets_data'] = ob_get_clean(); else: $content['tickets_data'] = 'เลขที่ท่านต้องการไม่มีในแผง'; endif;
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Tienes razón en que empty() devuelve TRUE para 0 .

En su lugar, podría usar filter_input() con el filtro FILTER_VALIDATE_INT aplicado.

Incluso podría simplificar todo con un mapa de rango...

 $str = implode("", array_map(function($i) { $val = filter_input( INPUT_POST, "n_$i", FILTER_VALIDATE_INT ); return $val === null || $val === false ? '.' : $val; }, range(1, 6)));
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!